package com.sunther.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunther.document.dao.ProjectHeaderDao;
import com.sunther.document.entity.ProjectHeaderEntity;
import com.sunther.document.service.ProjectHeaderService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author csb
 * @description: TODO
 * @date 2023/7/5 14:50
 */
@Service
public class ProjectHeaderServiceImpl extends ServiceImpl<ProjectHeaderDao, ProjectHeaderEntity> implements ProjectHeaderService {
    @Override
    public Set<String> getProjectIdsByUserId(Integer userId, Integer userType) {
        QueryWrapper<ProjectHeaderEntity> wrap = new QueryWrapper<>();
        wrap.eq("user_type",userType);
        wrap.eq("user_id",userId);
        Set<String> list = list(wrap).stream().map(ProjectHeaderEntity::getProjectId).collect(Collectors.toSet());
        return list;
    }

    @Override
    public Set<Integer> getUserIdsByProjectId(Set<Integer> ids, Integer userType) {
        QueryWrapper<ProjectHeaderEntity> wrap = new QueryWrapper<>();
        wrap.in("project_id",ids);
        wrap.eq("user_type",userType);
        Set<Integer> list = list(wrap).stream().map(ProjectHeaderEntity::getUserId).collect(Collectors.toSet());
        return list;
    }

    @Override
    public Map<String, Set<Integer>> getUserIdsByUserType(Set<String> ids,Integer userType) {
        HashMap<String, Set<Integer>> set = new HashMap<>();
        if(ids.size() > 0){
            QueryWrapper<ProjectHeaderEntity> wrap = new QueryWrapper<>();
            wrap.in("project_id",ids);
            wrap.eq("user_type",userType);
            List<ProjectHeaderEntity> list = list(wrap);
            Map<String, List<ProjectHeaderEntity>> map = list.stream().collect(Collectors.groupingBy(ProjectHeaderEntity::getProjectId));
            map.forEach((k,v)->{
                Set<Integer> collect = v.stream().map(ProjectHeaderEntity::getUserId).collect(Collectors.toSet());
                set.put(k,collect);
            });
        }
        return set;
    }
}
